package io.gitee.luoyuan800.tool.llm.bean;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class ContentMessage extends AbstractMessage<List<ContentMessage.Content>> {
    private List<Content> content;
    public ContentMessage(String msg, String image){
        content = new ArrayList<>();
        TextContent textContent = new TextContent();
        textContent.setText(msg);
        textContent.setType("text");
        content.add(textContent);
        if(StringUtils.isNotBlank(image)) {
            ImageContent imageContent = new ImageContent();
            imageContent.setType("image_url");
            if (image.startsWith("http")) {
                imageContent.setImageUrl(ImageUrl.fromUrl(image));
            } else {
                imageContent.setImageUrl(ImageUrl.formBase64(image));
            }
            content.add(imageContent);
        }
    }

    interface Content{

    }
    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
    public static class TextContent implements Content{
        private String type;
        private String text;
    }
    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
    public static class ImageContent implements Content{
        private String type;
        private ImageUrl imageUrl;
    }

    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
    public static class ImageUrl{
        private String url;
        public static ImageUrl formBase64(String base64Image){
            ImageUrl imageUrl = new ImageUrl();
            imageUrl.setUrl("data:image/jpeg;base64," + base64Image);
            return imageUrl;
        }

        public static ImageUrl fromUrl(String url){
            ImageUrl imageUrl = new ImageUrl();
            imageUrl.setUrl(url);
            return imageUrl;
        }
    }

}
